#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ll long long
#define ull unsigned long long
#define lld long long double
#define read(a) for(int i = 0 ; i < a.size();i++) cin >> a[i];
#define print(a) for(int i = 0 ; i < a.size();i++) cout << a[i] <<" ";
#define rep(i,x,n) for(int i = x ; i <=n; i++)
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int,int>
#define msi multiset<int>
#define si set<int>
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define in insert
#define er erase
#define all(x) x.begin(),x.end()
// Debugger
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
//void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
// Debugger end
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const bool Multiple = 0;
const int MAXN = 1e5 + 10;
vector<int> adj[MAXN];
vector<int> a(MAXN);
vector<int> s(MAXN);
int ans = 0 ;
void dfs(int cur , int par) {
int spar = 0 ;
if (par != -1) spar = s[par];
if (s[cur] == -1) {
int minv = 2e9;
for (auto it : adj[cur]) {
minv = min(minv, s[it]);
}
if (adj[cur].size() == 0) {
minv = spar;
}
s[cur] = minv;
}
for (auto it : adj[cur]) {
dfs(it, cur);
}
a[cur] = s[cur] - spar;
ans = ans + a[cur];
}
void solve() {
int n ;
cin >> n ;
for (int i = 2 ; i <= n ; i++) {
int x ;
cin >> x;
adj[x].pb(i);
}
for (int i = 1 ; i <= n; i++) {
cin >> s[i];
}
dfs(1, -1);
for (int i = 1 ; i <= n ; i++) {
if (a[i] < 0) {
cout << -1 << endl;
return;
}
}
cout << ans << endl;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1 ;
if (Multiple) cin >> t;
for (int i = 1 ; i <= t ; i++) {
//cout << "Case #" << i << ": ";
solve();
}
return 0 ;
}
1711D - Rain | 534A - Exam |
1472A - Cards for Friends | 315A - Sereja and Bottles |
1697C - awoo's Favorite Problem | 165A - Supercentral Point |
1493A - Anti-knapsack | 1493B - Planet Lapituletti |
747B - Mammoth's Genome Decoding | 1591C - Minimize Distance |
1182B - Plus from Picture | 1674B - Dictionary |
1426C - Increase and Copy | 520C - DNA Alignment |
767A - Snacktower | 1365A - Matrix Game |
714B - Filya and Homework | 31A - Worms Evolution |
1691A - Beat The Odds | 433B - Kuriyama Mirai's Stones |
892A - Greed | 32A - Reconnaissance |
1236D - Alice and the Doll | 1207B - Square Filling |
1676D - X-Sum | 1679A - AvtoBus |
1549A - Gregor and Cryptography | 918C - The Monster |
4B - Before an Exam | 545B - Equidistant String |